package com.alstudio.utils.mission.parser;

import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.mission.ALMissionHttpManager.MissionInfoCallback;
import com.alstudio.utils.mission.MissionInfo;
import com.alstudio.utils.mission.parser.base.BaseMissionXmlParser;
import com.alstudio.utils.xml.BaseXmlParser.XmlParseCallback;

public class TaskMainParser extends BaseMissionXmlParser implements
		XmlParseCallback {

	@Override
	public void parseXmlPackage(String stream, MissionInfoCallback listener)
			throws Exception {
		init(listener);
		startParse(stream, this);
	}

	@Override
	public void onTagStart(String tag) {
		// TODO Auto-generated method stub
		if (ITEM_TAG.equals(tag)) {
			info = new MissionInfo();
		} else if (ERROR_TAG.equals(tag)) {
			code = ALNumericUtils.parseInt(readText(), 120);
		}/* else if (TYPE_TAG.equals(tag)) {
			info.setId(ALNumericUtils.parseInt(readText(), 0));
		} else if (TYPE_NAME_TAG.equals(tag)) {
			info.setTitle(readText());
		} else if (TYPE_DESC_TAG.equals(tag)) {
			info.setDesc(readText());
		} else if (TYPE_STEP_STATUS_TAG.equals(tag)) {
			info.setStatus(readText());
		} else if (TYPE_ACTIVITY_TAG.equals(tag)) {
			int flag = ALNumericUtils.parseInt(readText(), 0);
			info.setLocked(true);
			if (flag == 1) {
				info.setLocked(false);
			}
		} else if (COMPLETE.equals(tag)) {
			info.setComplete(ALNumericUtils.parseInt(readText(), 0));
		} else if (DAILY_COMPLETE.equals(tag)) {
			dailyComplete = ALNumericUtils.parseInt(readText(), 0);
		}*/
		// 猫呼任务

		else if (DESC_TAG.equals(tag)) {
			info.setDesc(readText());
		} else if (TASK_ID_TAG.equals(tag)) {
			info.setTask_id(readText());
		} else if (STATUS_TAG.equals(tag)) {
			info.setStatus(readText());
		} else if (EXP_TAG.equals(tag)) {
			info.setAddexp(readText());
		} else if (GOLD_TAG.equals(tag)) {
			info.setAddgold(readText());
		} else if (GIFT_ID_TAG.equals(tag)) {
			info.setGiftId(readText());
		} else if (GIFT_NAME_TAG.equals(tag)) {
			info.setGiftName(readText());
		} else if (FILEID_TAG.equals(tag)) {
			info.setFileId(readText());
		}
	}

	@Override
	public void onTagEnd(String tag) {
		// TODO Auto-generated method stub
		if (ITEM_TAG.equals(tag)) {
			ALLog.d("任务名 " + info.getTitle());
			tasks.add(info);
		}
	}

	@Override
	public void onDocumentStart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDocumentEnd() {
		// TODO Auto-generated method stub
		onCallback();
	}

	@Override
	protected void onCallback() {
		// TODO Auto-generated method stub
		super.onCallback();
		if (mListener != null) {
			mListener.onGetTaskList(tasks, code, dailyComplete);
		}
	}

}
